//
//  SuperviseHomeCell.swift
//  SBSafetyApp
//
//  Created by Aimeow on 2022/11/22.
//

import Foundation
import UIKit

enum SuperviseEnum: Int {
    case newTask = 1
    case task = 2
    case arrage = 3
    case reCheck = 4
    case statitics = 5
}

class SuperviseHomeModel: NSObject {
    var type: SuperviseEnum = .newTask
    var icon: String = ""
    var title: String = ""
    
    init(type: SuperviseEnum, icon: String, title: String) {
        self.type = type
        self.icon = icon
        self.title = title
    }
}

class SuperviseHomeCell: UITableViewCell {
    let countL = PaddingLabel()
    
    var count: Int64? = 0 {
        didSet {
            countL.text = "\(count ?? 0)"
            countL.isHidden = (count == nil || count == 0)
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .bg
        selectionStyle = .none
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func buildData(model: SuperviseHomeModel) {
        titleLabel.text = model.title
        iconImageView.image = UIImage.init(named: model.icon)
    }
    
    func setupUI() {
        addSubview(baseV)
        baseV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
            make.top.equalToSuperview().offset(5)
            make.bottom.equalToSuperview().offset(-5)
        }
        
        baseV.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(34)
        }

        baseV.addSubview(iconImageView)
        iconImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-28)
            make.width.height.equalTo(44)
        }
        
        countL.isHidden = count == 0
        countL.insets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
        countL.textColor = .white
        countL.textAlignment = .center
        countL.font = .systemFont(ofSize: 15)
        countL.layer.masksToBounds = true
        countL.layer.cornerRadius = 10
        countL.backgroundColor = .hex("#FF0000")
        baseV.addSubview(countL)
        countL.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.top).offset(-13)
            make.left.equalTo(titleLabel.snp.right).offset(2)
            make.height.equalTo(20)
            make.width.greaterThanOrEqualTo(18)
        }
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        super.setHighlighted(highlighted, animated: animated)
        if highlighted {
            baseV.backgroundColor = .white.withAlphaComponent(0.6)
        } else {
            baseV.backgroundColor = .white
        }
    }
    
    lazy var baseV: UIView = {
        let view = UIView.createBase()
        return view
    }()
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = UIView.ContentMode.scaleAspectFit
        return imageView
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 18)
        label.textColor = .hex("#333333")
        return label
    }()
    
}

